/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.DbMapper;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.File;
import java.io.InputStream;


/**
 * <p></p>
 * <p>Powered by webrx On 2023-04-18 15:11:15</p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

public class Demo {
    public static void main(String[] args) {
        //logback log4j2
        try (var is = Resources.getResourceAsStream("mybatis-config.xml")) {
            var sf = new SqlSessionFactoryBuilder().build(is);

            Configuration cfg = sf.getConfiguration();

            //-----------------------------批量扫描src/main/resources/mapper/*.xml文件
            String xml = "mapper/";
            String p = Thread.currentThread().getContextClassLoader().getResource(xml).getPath();
            File f = new File(p);
            for (String s : f.list()) {
                var t = xml + s;
                InputStream is2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(t);
                new XMLMapperBuilder(is2, cfg, t, cfg.getSqlFragments()).parse();
            }
            //-----------------------------

            var ss = sf.openSession();
            BookMapper bm = ss.getMapper(BookMapper.class);

            System.out.println(bm.queryBook());

            DbMapper dm = ss.getMapper(DbMapper.class);
            System.out.println(dm.version());


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
